##             ##
# Module loader #
##             ##

import os
import sys

modules=[]
class ModuleLoader():
    def __init__(self):
        sys.path.append("modules/")

    def loadModules(self):
        global modules
        modulenames=[]
        mlist=os.listdir("modules/")
        for m in mlist:
            if(m.find(".pyc")==-1): #fugly code
                if(m.find(".py")>-1):
                    modulenames.append(m[0:m.find(".py")])
        modules=map(__import__, modulenames)
    def	addModule(self,name):
        global modules
        modulenames=[]
        mlist=os.listdir("modules/")
        for m in mlist:
            if(m.find(name+".py")>-1):
                modules.append(__import__(name))
            
    def delModule(self,name):
        global modules
        for m in modules:
            if(m.name==name):
                modules.remove(m)
    def useModule(self,cmd):
        global modules
        print "useModule called\n arguments are "+cmd
        #split command, # as delimiter [1] voor module naam 
        cmd=cmd.strip()
        args=cmd.split("#")
        for m in modules:
            if(m.name==args[0]):
		print "Arg is "+args[1]
                return m.do(args[1])
                
    def	listModules(self):
        global modules
        list=[]
        for m in modules:
            name= m.name
            list.append(name)
        return list
